package com.ir.mirrajabi.searchdialog.sample.customdialog;

import com.ir.mirrajabi.searchdialog.sample.ResourceTable;

import ir.mirrajabi.searchdialog.StringsHelper;
import ir.mirrajabi.searchdialog.core.BaseSearchDialogCompat;
import ir.mirrajabi.searchdialog.core.SearchResultListener;
import ir.mirrajabi.searchdialog.core.Searchable;
import ir.mirrajabi.searchdialog.util.ResTUtil;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.render.render3d.ViewHolder;
import ohos.agp.text.RichText;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索dialog适配器
 * @param <T> T
 */
public class ContactModelAdapter<T extends Searchable> extends BaseItemProvider {
    /**
     * 上下文
     */
    protected Context mContext;
    private List<T> mItems = new ArrayList<>();
    private LayoutScatter mLayoutInflater;
    private int mLayout;
    private SearchResultListener mSearchResultListener;
    private AdapterViewBinder<T> mViewBinder;
    private String mSearchTag;
    private boolean mHighlightPartsInCommon = true;

    private BaseSearchDialogCompat mSearchDialog;

    public ContactModelAdapter(Context context, int layout, List<T> items) {
        this(context, layout, null, items);
    }

    public ContactModelAdapter(Context context, AdapterViewBinder<T> viewBinder, int layout, List<T> items) {
        this(context, layout, viewBinder, items);
    }

    public ContactModelAdapter(Context context, int layout, AdapterViewBinder<T> viewBinder, List<T> items) {
        this.mContext = context;
        this.mLayoutInflater = LayoutScatter.getInstance(context);
        this.mItems = items;
        this.mLayout = layout;
        this.mViewBinder = viewBinder;
    }

    /**
     * getItems
     * @return List
     */
    public List<T> getItems() {
        return mItems;
    }

    /**
     * setItems
     * @param objects objects
     */
    public void setItems(List<T> objects) {
        this.mItems = objects;
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        if (mItems != null) {
            return mItems.size();
        } else {
            return 0;
        }
    }

    /**
     * getItem
     * @param position position
     * @return T
     */
    public T getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        component = mLayoutInflater.parse(mLayout, componentContainer, false);

        Text textName = (Text) component.findComponentById(ResourceTable.Id_txt_item);
        T object = getItems().get(position);
        Image image = (Image) component.findComponentById(ResourceTable.Id_img_icon);
        image.setVisibility(Component.VISIBLE);
        if (mSearchTag != null && mHighlightPartsInCommon) {
            RichText richText =
                    StringsHelper.highLightLCS(
                                    object.getTitle(),
                                    getSearchTag(),
                                    ResTUtil.getColor(mContext, ResourceTable.Color_black),
                                    ResTUtil.getColor(mContext, ResourceTable.Color_accent),
                                    AttrHelper.fp2px(14, mContext))
                            .build();

            textName.setRichText(richText);
        } else {
            textName.setText(object.getTitle());
        }
        if (mSearchResultListener != null) {
            component.setClickedListener(
                    new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            mSearchResultListener.onSelected(mSearchDialog, object, position);
                        }
                    });
        }
        return component;
    }

    /**
     * setViewBinder
     * @param viewBinder viewBinder
     * @return ContactModelAdapter
     */
    public ContactModelAdapter<T> setViewBinder(AdapterViewBinder<T> viewBinder) {
        this.mViewBinder = viewBinder;
        return this;
    }

    /**
     * 结果监听
     * @return SearchResultListener
     */
    public SearchResultListener getSearchResultListener() {
        return mSearchResultListener;
    }

    /**
     * setSearchResultListener
     * @param searchResultListener searchResultListener
     */
    public void setSearchResultListener(SearchResultListener searchResultListener) {
        this.mSearchResultListener = searchResultListener;
    }

    /**
     * getSearchTag
     * @return String
     */
    public String getSearchTag() {
        return mSearchTag;
    }

    /**
     * setSearchTag
     * @param searchTag searchTag
     * @return ContactModelAdapter
     */
    public ContactModelAdapter setSearchTag(String searchTag) {
        mSearchTag = searchTag;
        return this;
    }

    /**
     * isHighlightPartsInCommon
     * @return boolean
     */
    public boolean isHighlightPartsInCommon() {
        return mHighlightPartsInCommon;
    }

    /**
     * setHighlightPartsInCommon
     * @param highlightPartsInCommon highlightPartsInCommon
     * @return ContactModelAdapter
     */
    public ContactModelAdapter setHighlightPartsInCommon(boolean highlightPartsInCommon) {
        mHighlightPartsInCommon = highlightPartsInCommon;
        return this;
    }

    /**
     * setSearchDialog
     * @param searchDialog searchDialog
     * @return ContactModelAdapter
     */
    public ContactModelAdapter setSearchDialog(BaseSearchDialogCompat searchDialog) {
        mSearchDialog = searchDialog;
        return this;
    }

    /**
     * AdapterViewBinder
     * @param <T> T
     */
    public interface AdapterViewBinder<T> {
        void bind(ViewHolder holder, T item, int position);
    }
}
